package com.doyens.seckill.task;

import com.doyens.common.utils.IdWorker;
import com.doyens.seckill.mapper.SeckillGoodsMapper;
import com.doyens.seckill.pojo.SeckillGoods;
import com.doyens.seckill.pojo.SeckillOrder;
import com.doyens.seckill.utils.SeckillStatus;
import com.sun.scenario.effect.impl.sw.sse.SSEBlend_SRC_OUTPeer;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Component;

import java.util.Date;

@Component
public class MultiThreadingCreateOrder {
    @Autowired
    private RedisTemplate redisTemplate;

    @Autowired
    private IdWorker idWorker;

    @Autowired
    private SeckillGoodsMapper seckillGoodsMapper;
   /***
         * 多线程下单操作
         */
    @Async
    public void createOrder(){
        try {
            System.out.println("准备执行....");
            //从redis消息队列中获取信息
            SeckillStatus seckillStatus = (SeckillStatus) redisTemplate.boundListOps("seckillOrderQueue").rightPop();


            String time = seckillStatus.getTime();
            Long id = seckillStatus.getGoodsId();
            String username = seckillStatus.getUsername();

            //获取商品数据
            SeckillGoods seckillGoods = (SeckillGoods) redisTemplate.boundHashOps("seckillGoods_" + time).get(id);
            Thread.sleep(10000);
            System.out.println(username + "======"+Thread.currentThread().getId()+"==="+"查询到的商品库存:="+seckillGoods.getStockCount());

            //如果没有库存要抛出异常
            if (seckillGoods == null || seckillGoods.getStockCount() <=0){
                throw  new RuntimeException("已售罄");
            }

            if(seckillGoods !=null && seckillGoods.getStockCount() > 0){
                //有库存
                SeckillOrder seckillOrder = new SeckillOrder();
                seckillOrder.setId(idWorker.nextId());
                seckillOrder.setSeckillId(id);
                seckillOrder.setMoney(seckillGoods.getCostPrice());
                seckillOrder.setSellerId(seckillGoods.getSellerId());
                seckillOrder.setUserId(username);
                seckillOrder.setCreateTime(new Date());
                seckillOrder.setStatus("0");
                //秒杀订单存入到redis中
                redisTemplate.boundHashOps("seckillOrder").put(username,seckillOrder);
                //减库存
                seckillGoods.setStockCount(seckillGoods.getStockCount()-1);
                //如果减完库存 库存为0了   删除redis中的商品,同步数据库
                if (seckillGoods.getStockCount()<=0){
                    seckillGoodsMapper.updateByPrimaryKey(seckillGoods);
                    //清除redis
                    redisTemplate.boundHashOps("seckillGoods_"+time).delete(id);
                }else{ //如果大于0  需要同步redis中的数据
                    redisTemplate.boundHashOps("seckillGoods_"+time).put(id,seckillGoods);
                }
                //更改订单状态
                seckillStatus.setOrderId(seckillOrder.getId());
                seckillStatus.setMoney(seckillOrder.getMoney().floatValue());
                seckillStatus.setStatus(2);//抢单成功,待支付
                //存到redis中
                redisTemplate.boundHashOps("userQueueStatus").put(username,seckillStatus);
            }
            System.out.println("开始执行....");
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
     }
}